package com.pingsky.apm.agent.core.service.message;

import com.pingsky.apm.agent.core.service.metadata.AgentInfo;
import com.pingsky.apm.network.language.agent.v1.PAgentInfo;

public class GRPCMessageSerializer implements MessageSerializer {

    public static final String AGENT_INFO_TOPIC = "PING_AGENT_INFO";

    public static final String TRACE_TOPIC = "PING_SEGMENT";

    @Override
    public TopicMessage toMessage(Object message) {
        if (message instanceof AgentInfo) {
            AgentInfo agentInfo = (AgentInfo) message;
            return buildAgentInfo(agentInfo);
        }
        return null;
    }

    private TopicMessage buildAgentInfo(AgentInfo agentInfo) {
        PAgentInfo pAgentInfo = PAgentInfo.newBuilder()
                .setAgentId(agentInfo.getAgentId())
                .setApplicationName(agentInfo.getApplicationName())
                .setStartTime(agentInfo.getStartTime())
                .setPid(agentInfo.getPid())
                .setHostIp(agentInfo.getHostIp())
                .setHostName(agentInfo.getHostName())
                .setJvmVersion(agentInfo.getJvmVersion())
                .setAgentVersion(agentInfo.getAgentVersion()).build();
        return new TopicMessage(AGENT_INFO_TOPIC, pAgentInfo);
    }

    @Override
    public void prepare() throws Throwable {

    }

    @Override
    public void start() throws Throwable {

    }
}
